package com.bootdo.common.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.Conts;
import com.bootdo.common.huobi.api.ApiClient;
import com.bootdo.common.huobi.response.Trade;
import com.bootdo.common.huobi.response.TradeResponse;
import com.bootdo.common.utils.RedisUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SimpleTrigger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import springfox.documentation.service.ApiListing;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class SymbolPriceJob  implements Job {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private static String API_KEY = "de2162ba-931a9dda-88f3caa2-uymylwhfeg";

    private static String API_SECRET = "edcc7848-06c8ea9a-3995ea0e-07db4";

    @Autowired
    private RedisUtils redisTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        ApiClient client = new ApiClient(API_KEY, API_SECRET);
        List<String> symbols = new ArrayList<String>();
        symbols.add("eosusdt");
        symbols.add("btcusdt");
        symbols.add("ethusdt");
        for (int i = 0 ; symbols.size() > i; i++){
            TradeResponse response =  client.trade(symbols.get(i));
            if (response.getStatus().equals("ok")) {
                Trade<List<Map<String,Object>>> trade =  response.getTick();
                List<Map<String,Object>> listmap =  trade.getData();
                Map<String,Object> map = listmap.get(0);
               String price = String.valueOf( map.get("price")) ;
               redisTemplate.set(Conts.SYMBOL_PRICE_KEY+symbols.get(i), price );
            }
        }
        logger.info("价格已缓存完毕！！");

    }
}
